using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Christian.Helle.PocketNews.PocketPC.Controls;

namespace Christian.Helle.PocketNews.PocketPC
{
    public partial class frmSettings : FormEx
    {
        private bool saved = false;
        private bool changed = false;
        private bool hasLoaded = false;
        private bool cancelled = false;
        private bool firstRun = false;

        public frmSettings()
        {
            using (new WaitCursor())
            {
                InitializeComponent();
                //EnableSmartSIP(); 
            }
        }

        public frmSettings(bool firstRun)
            : this()
        {
            this.firstRun = firstRun;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            changed = false;
            cancelled = true;

            DialogResult = DialogResult.Cancel;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            using (new WaitCursor())
            {
                Configuration.SenderName = tbName.Text;
                Configuration.SenderEmail = tbEmail.Text;
                Configuration.Signature = tbSignature.Text;
                Configuration.NntpServer = tbNewsServer.Text;
                Configuration.AccountName = tbAccountName.Text;
                Configuration.Password = tbPassword.Text;
                Configuration.Signature = tbSignature.Text;
                Configuration.AutoInsertSignature = cbAutoInsertSignature.Checked;
                Configuration.NntpRequiresLogon = cbRequireLogon.Checked;

                if (changed || firstRun)
                    Configuration.SaveChanges();

                saved = true;

                DialogResult = DialogResult.OK;
            }
        }

        private void cbRequireLogon_CheckStateChanged(object sender, EventArgs e)
        {
            lblAccountName.Visible = cbRequireLogon.Checked;
            lblPassword.Visible = cbRequireLogon.Checked;
            tbAccountName.Visible = cbRequireLogon.Checked;
            tbPassword.Visible = cbRequireLogon.Checked;
        }

        private void frmSettings_Closing(object sender, CancelEventArgs e)
        {
            if (cancelled)
                return;

            StringBuilder warning = new StringBuilder();
            bool missingField = false;

            if (string.IsNullOrEmpty(tbName.Text))
            {
                missingField = true;
                warning.Append("\nName");
            }
            if (string.IsNullOrEmpty(tbEmail.Text))
            {
                missingField = true;
                warning.Append("\nEmail");
            }

            if (string.IsNullOrEmpty(tbSignature.Text))
            {

                missingField = true;
                warning.Append("\nSignature");
            }

            if (string.IsNullOrEmpty(tbNewsServer.Text))
            {

                missingField = true;
                warning.Append("\nNNTP Server");
            }

            if (cbRequireLogon.Checked)
            {
                if (string.IsNullOrEmpty(tbAccountName.Text))
                {

                    missingField = true;
                    warning.Append("\nAccount Name");
                }

                if (string.IsNullOrEmpty(tbPassword.Text))
                {

                    missingField = true;
                    warning.Append("\nPassword");
                }
            }

            if (missingField)
            {
                warning.Insert(0, "The following fields are blank:\n");
                warning.Append("\n\nDo you wish to continue?");

                DialogResult result = MessageBox.Show(
                    warning.ToString(),
                    "Missing Fields",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Asterisk,
                    MessageBoxDefaultButton.Button1);

                e.Cancel = (result == DialogResult.No);
            }
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            tbName.Text = Configuration.SenderName;
            tbEmail.Text = Configuration.SenderEmail;
            tbSignature.Text = Configuration.Signature;
            tbNewsServer.Text = Configuration.NntpServer;
            tbAccountName.Text = Configuration.AccountName;
            tbPassword.Text = Configuration.Password;
            cbAutoInsertSignature.Checked = Configuration.AutoInsertSignature;
            cbRequireLogon.Checked = Configuration.NntpRequiresLogon;
                        
            if (string.IsNullOrEmpty(tbNewsServer.Text))
                tbNewsServer.Text = "msnews.microsoft.com";

            hasLoaded = true;
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            if (!hasLoaded)
                return;

            changed = true;
        }

        private void cbAutoInsertSignature_CheckStateChanged(object sender, EventArgs e)
        {
            if (!hasLoaded)
                return;

            changed = true;
        }

        private void TransitionEffect_CheckedChanged(object sender, EventArgs e)
        {
            if (!hasLoaded)
                return;

            changed = true;
        }
    }
}